package scsc.pm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.exe.TaskInstance;

import scsc.pub.data.TaskItem;
import scsc.pub.data.TaskItemInstance;
import scsc.pub.db.DBManage;
import scsc.pub.db.PageListData;
import scsc.pub.exception.UnifyUserException;
import scsc.pub.tools.StringUtil;
import scsc.web.system.data.TaskItemData;
import scsc.web.system.data.TaskItemInstanceData;

public class SysTaskItem {
	public void createTaskItemInstance(DBManage dbmanage, List taskInstanceList)
			throws UnifyUserException {
		for (int i = 0; i < taskInstanceList.size(); i++) {
			String sqlStr = "select new scsc.web.system.data.TaskItemInstanceData(taskitem.name,taskitem.url,taskitem.needDo) from scsc.pub.data.TaskItem as taskitem where taskitem.taskId="
					+ ((TaskInstance) taskInstanceList.get(i)).getTask()
							.getId();
			List result = dbmanage.sqlQuery(sqlStr);
			sqlStr = "from scsc.pub.data.TaskItemInstance as taskitem where taskitem.taskInstanceId="
					+ ((TaskInstance) taskInstanceList.get(i)).getId();
			int exist = dbmanage.sqlQueryCount(sqlStr);
			for (int j = 0; j < result.size() && exist == 0; j++) {
				TaskItemInstanceData taskItemInstanceDate = (TaskItemInstanceData) result
						.get(j);
				TaskItemInstance data = new TaskItemInstance();
				data.setTaskInstanceId(new Long(
						((TaskInstance) taskInstanceList.get(i)).getId()));
				data.setName(taskItemInstanceDate.getName());
				data.setUrl(taskItemInstanceDate.getUrl());
				data.setNeedDo(taskItemInstanceDate.getNeedDo());
				data.setIsdone("0");
				dbmanage.sqlSave(data);
			}
		}
	}

	public List selectNeedDoList(DBManage dbmanage, TaskInstance taskInstance) {
		String sqlStr = "select new scsc.web.system.data.TaskItemInstanceData(taskitem.id,taskitem.name,taskitem.url,taskitem.needDo,taskitem.isdone) from scsc.pub.data.TaskItemInstance as taskitem where 1=1";
		if (taskInstance != null) {
			sqlStr = sqlStr + " and taskitem.taskInstanceId ="
					+ taskInstance.getId() + " and taskitem.needDo ='1'";
		}
		List result = dbmanage.sqlQuery(sqlStr);
		return result;
	}

	public List selectNoNeedList(DBManage dbmanage, TaskInstance taskInstance) {
		String sqlStr = "select new scsc.web.system.data.TaskItemInstanceData(taskitem.id,taskitem.name,taskitem.url,taskitem.needDo,taskitem.isdone) from scsc.pub.data.TaskItemInstance as taskitem where 1=1";
		if (taskInstance != null) {
			sqlStr = sqlStr + " and taskitem.taskInstanceId ="
					+ taskInstance.getId() + " and taskitem.needDo ='0'";
		}
		List result = dbmanage.sqlQuery(sqlStr);
		return result;
	}

	public boolean isAllDone(DBManage dbmanage, TaskInstance taskInstance) {
		String sqlStr = "from scsc.pub.data.TaskItemInstance as taskitem where 1=1";
		if (taskInstance != null) {
			sqlStr = sqlStr + " and taskitem.taskInstanceId ="
					+ taskInstance.getId()
					+ " and taskitem.needDo ='1' and taskitem.isdone='0'";
		}
		if (dbmanage.sqlQueryCount(sqlStr) > 0) {
			return false;
		} else {
			return true;
		}
	}

	public void isDone(DBManage dbmanage, Long taskItemInstanceId)
			throws UnifyUserException {
		String sqlStr = "update scsc.pub.data.TaskItemInstance as taskitem set taskitem.isdone='1' where 1=1";
		if (taskItemInstanceId != null) {
			sqlStr = sqlStr + " and taskitem.id =" + taskItemInstanceId;
		}
		dbmanage.createQuery(sqlStr);
	}

	public void createTaskItem(DBManage dbmanage, TaskItemData taskItemData)
			throws UnifyUserException {
		TaskItem data = new TaskItem();
		data.setTaskId(taskItemData.getTaskId());
		data.setName(StringUtil.trim(taskItemData.getName()));
		data.setUrl(StringUtil.trim(taskItemData.getUrl()));
		data.setNeedDo(StringUtil.trim(taskItemData.getNeedDo()));
		dbmanage.sqlSave(data);
		taskItemData.setId(data.getId());
	}

	public void deleteTaskItem(DBManage dbmanage, String id)
			throws UnifyUserException {
		String hql = "delete from scsc.pub.data.TaskItem where id=" + id;
		dbmanage.sqlDelete(hql);
	}

	public void modifyTaskItem(DBManage dbmanage, TaskItemData taskItemData)
			throws UnifyUserException {
		TaskItem data = new TaskItem();
		data.setId(taskItemData.getId());
		data.setTaskId(taskItemData.getTaskId());
		data.setName(StringUtil.trim(taskItemData.getName()));
		data.setUrl(StringUtil.trim(taskItemData.getUrl()));
		data.setNeedDo(StringUtil.trim(taskItemData.getNeedDo()));
		dbmanage.sqlUpdate(data);
	}

	public void selectTaskItems(DBManage dbmanage, String taskId, String name,
			PageListData footer) {
		String sqlStr = "select new scsc.web.system.data.TaskItemData(taskitem.id,taskitem.taskId,taskitem.name,taskitem.url,taskitem.needDo) from scsc.pub.data.TaskItem as taskitem where 1=1";
		if (taskId.length() > 0) {
			sqlStr = sqlStr + " and taskitem.taskId = " + taskId;
		}
		if (name.length() > 0) {
			sqlStr = sqlStr + " and taskitem.name like '%" + name + "%'";
		}
		dbmanage.sqlQueryPage(sqlStr, footer);
	}

	public ArrayList selectTaskItems(DBManage dbmanage, Long id) {
		String sqlStr = "select new scsc.web.system.data.TaskItemData(taskitem.id,taskitem.taskId,taskitem.name,taskitem.url,taskitem.needDo) from scsc.pub.data.TaskItem as taskitem where 1=1";
		if (id != null) {
			sqlStr = sqlStr + " and taskitem.id = " + id;
		}
		List result = dbmanage.sqlQuery(sqlStr);
		return (ArrayList) result;
	}
}
